<template>
    <div class="changepwd">
        <p class="head">
            <span class="lf">修改密码</span>
        </p>
        <p v-show="showTip"  class="showTip" >{{tip}}</p>
        <input type="text" placeholder="请输入用户名" class="mobile" v-model="mobile">
        <input type="text" placeholder="请输入原密码" class="pwd" v-model="oldpwd">
        <input type="password" placeholder="请输入新密码" class="newPwd" v-model="newpwd">
        <input type="password" placeholder="请确认新密码" class="newPwd" v-model="newpwd2">
        <button class="changeBtn" @click="changepwd">确认修改</button>
    </div>
</template>

<script>
import { setCookie,getCookie,delCookie } from '../assets/cookie.js'
import {url} from "./url.js"
    export default {
        data(){
            return{
                showTip:"false",
                tip:"",
                mobile:"",
                oldpwd:"",
                newpwd:"",
                newpwd2:""
            }
        },
        methods:{
            gohome(){
                this.$router.push('/admin')
            },
            changepwd(){
                if(this.mobile==''||this.oldpwd==''||this.newpwd==''||this.newpwd2==''){
                    this.tip = "请输入完整信息"
                    this.showTip = true
                }else if(this.newpwd!==this.newpwd2){
                    console.log(this.newpwd)
                    this.tip ="两次输入密码不同"
                }else{
                    var self = this;
                    var oldpwd = self.$md5(self.oldpwd)
                    var newpwd = self.$md5(self.newpwd)
                    var data={'mobile':self.mobile,'oldpwd':oldpwd,'newpwd':newpwd}
                    self.$http.put(url+'/users/users',
                    // self.$http.put('http://localhost:7777/users/users',
                    data,
                    // { headers: {
                    // "Access-Control-Allow-Origin": "*",
                    // }}
                    ).then(function (res) {
                            console.log(res)
                            var code = res.data.code;
                            var msg = res.data.msg;
                            if(code==400){
                                self.tip = msg
                                self.showTip = true
                            }else if(code == 200){
                                self.tip = msg
                                self.showTip = true
                                setCookie(self.mobile,'',1000*60)
                                setTimeout(function(){
                                    self.$router.push('/admin')
                                }.bind(self),1000)
                            }
                        }).catch(function (error) {
                    });
                }
            }
        }
    }
</script>

<style scoped>
.lf{float:left}
.rt{float:right}
.changepwd{
  margin:0 auto;
  width:300px;
}
.pwd{
  box-sizing: border-box;
  width: 100%;
  padding: 10px 10px;
  margin: 10px 0;
}
.newPwd{
  box-sizing: border-box;
  width: 100%;
  padding: 10px 10px;
  margin: 10px 0;
}
.changeBtn{
  display: block;
  width:100%;
  padding: 10px 10px;
  margin: 10px 0;
  background:green;
  color:#ddd;
  cursor:pointer;
}
.mobile{
  box-sizing: border-box;
  width: 100%;
  padding: 10px 10px;
  margin: 10px 0;
}
.head{
     width:300px;
     height:44px;
     margin:10px auto;
}
.head img{
    height:44px;
    width:44px;
}
.head span{
    text-align: center;
    font-size: 30px;
    margin-left: 50px;
    height:44px;
    line-height: 44px;
}
.showTip{
    color:red;
    margin:5px auto;
}
</style>